import json
import os
from control.models.User import User
from unit.models import *


# 创建一个superadmin，密码是superadmin1234
user_entity = User.objects.filter(user_account="superadmin").first()
if user_entity is None:
    user_entity = User.objects.create(
        user_account="superadmin",
        user_name="superadmin",
        password="C6IDQGUT42vVUX0neDCF2TB2kD42tBWwp4AGqp3DenmkTYI9urFbhJomuOMcI25De98AjQJPBT07AUW5oQdYeHfeQKr/uhR6OABnKu+/uof5viBguKPdDqkc/znPMjADdt66dxVi/yeur8EahDaUCvhssdB4HkJLxteCLGlEDZ/AJe1BiTBCgyN+7ZLCEjxXD4lKbI5mT9oWEQ+Pd1rE7fY/jmuDh6Nnz2YO7/0WvQA39KESBVzrVQ5bmEVSnUNkQqfzR8DkylHQ54swmNGXFog9xd7q0gjd2VrsBfE6qD4y+iJThQlALLh1f9QdVthAcuepLUKW+PI/mmyllQTufw=="
    )


def init_unit_prefix(prefix_data):
    for prefix_item in prefix_data:

        prefix_id = "P"+"_" + \
            str(prefix_item["base"])+"_"+str(prefix_item["power"])
        if UnitPrefix.objects.filter(prefix_id=prefix_id).first():
            continue
        UnitPrefix.objects.create(
            prefix_name=prefix_item["prefix_name_ch"],
            prefix_name_en=prefix_item["prefix_name_en"],
            prefix_id=prefix_id,
            base=prefix_item["base"],
            power=prefix_item["power"],
            symbol=prefix_item["symbol"],
            symbol_ascii=prefix_item["symbol_ascii"],
            symbol_html=prefix_item["symbol_html"],
            symbol_latex=prefix_item["symbol_latex"],
            symbol_unicode=prefix_item["symbol_unicode"],
            description=prefix_item["description"],
            comment=prefix_item["comment"],
            create_person=user_entity,
            modify_person=user_entity
        )


def init_unit_basic_dimension(basic_dimension_data):
    for basic_dimension_item in basic_dimension_data:
        if BasicDimension.objects.filter(dimension_name=basic_dimension_item["dimension_name_ch"]).first():
            continue
        BasicDimension.objects.create(
            dimension_name=basic_dimension_item["dimension_name_ch"],
            dimension_name_en=basic_dimension_item["dimension_name_en"],
            symbol=basic_dimension_item["symbol"],
            symbol_ascii=basic_dimension_item["symbol_ascii"],
            symbol_html=basic_dimension_item["symbol_html"],
            symbol_latex=basic_dimension_item["symbol_latex"],
            symbol_unicode=basic_dimension_item["symbol_unicode"],
            basic_unit=basic_dimension_item["basic_unit"],
            description=basic_dimension_item["description"],
            create_person=user_entity,
            modify_person=user_entity,
            is_standard=True
        )


def init_unit_dimension(dimension_data):
    for dimension_item in dimension_data:
        if Dimension.objects.filter(dimension_id=dimension_item["dimension_id"]).first():
            continue
        dimension1 = Dimension.objects.create(
            dimension_id=dimension_item["dimension_id"],
            basic_unit=dimension_item["dimension_unit"],
            create_person=user_entity,
            modify_person=user_entity
        )
        for key, value in dimension_item["dimension_composition"].items():
            basic_dimension1 = BasicDimension.objects.filter(
                symbol=key).first()
            DimensionMap.objects.create(
                basic_dimension=basic_dimension1,
                dimension=dimension1,
                power=value,
            )


def init_unit_unit(unit_data):
    for unit_item in unit_data:
        if Unit.objects.filter(symbol=unit_item["symbol"]).first():
            continue
        dimension1 = Dimension.objects.filter(
            dimension_id=unit_item["dimension"]).first()
        if unit_item["system"] == 1:
            unit1 = Unit.objects.create(
                unit_name=unit_item["unit_name_zh"],
                unit_name_en=unit_item["unit_name_en"],
                symbol=unit_item["symbol"],
                dimension=dimension1,
                create_person=user_entity,
                modify_person=user_entity,
                is_system=True
            )
        else:
            unit1 = Unit.objects.create(
                unit_name=unit_item["unit_name_zh"],
                unit_name_en=unit_item["unit_name_en"],
                symbol=unit_item["symbol"],
                dimension=dimension1,
                create_person=user_entity,
                modify_person=user_entity,
                is_system=False
            )
            
        UnitSymbol.objects.create(
            symbol=unit_item["symbol"],
            root_unit=unit1,
            create_person=user_entity,
            modify_person=user_entity,
            is_system=True
        )

        if unit_item["system"] == 0:
            root_unit1 = Unit.objects.filter(
                symbol=unit_item["unit_basic_symbol"]).first()
            UnitConversion.objects.create(
                base_unit=root_unit1,
                target_unit=unit1,
                factor=unit_item["unit_factor"],
                offset=unit_item["unit_offset"],
                create_person=user_entity,
                modify_person=user_entity
            )


def init_unit_quantity(quantity_data):
    for quantity_item in quantity_data:
        if Quantity.objects.filter(quantity_name=quantity_item["quantity_name_zh"]).first():
            continue
    #     {
    #   "quantity_name_en": "linear expansion coefficient",
    #   "quantity_name_zh": "线[膨]胀系数",
    #   "dimension": "D_𝝧⁻¹",
    #   "unit": ["K⁻¹", "K^-1"]
    # },
        dimension1 = Dimension.objects.filter(
            dimension_id=quantity_item["dimension"]).first()
        quantity1 = Quantity.objects.create(
            quantity_name = quantity_item["quantity_name_zh"],
            quantity_name_en = quantity_item["quantity_name_en"],
            quantity_type = "NORMAL",
            dimension = dimension1,
            create_person = user_entity,
            modify_person = user_entity,
        )
        for unit_item in quantity_item["unit"]:
            unit1 = Unit.objects.filter(symbol=unit_item).first()
            if not QuantityUnitMap.objects.filter(quantity=quantity1, unit=unit1).first():
                QuantityUnitMap.objects.create(
                    quantity = quantity1,
                    unit = unit1
                )
    
def init_quantity_with_all_unit():
    # 获取所有量纲并为它们分配一个包含所有单位的量
    dimension_list = Dimension.objects.filter(valid_flag=True)
    for dimension_item in dimension_list:
        quantity_name = "全单位量"+"("+dimension_item.dimension_id+")"
        quantity1 = Quantity.objects.filter(quantity_name=quantity_name).first()
        if quantity1:
            continue
        quantity1 = Quantity.objects.create(
            quantity_name = quantity_name,
            quantity_name_en = "All unit quantity" + "("+dimension_item.dimension_id+")",
            quantity_type = "ALL_UNIT",
            dimension = dimension_item,
            create_person = user_entity,
            modify_person = user_entity,
        )
        # 获取该量纲下的所有单位
        unit_list = Unit.objects.filter(dimension=dimension_item, valid_flag=True)
        for unit_item in unit_list:
            if not QuantityUnitMap.objects.filter(quantity=quantity1, unit=unit_item).first():
                QuantityUnitMap.objects.create(
                    quantity = quantity1,
                    unit = unit_item
                )
    
    
    
    
    

def init_unit_dataV2():

    data_file = "files/unit_dataV2/unit_data_v2.json"
    if not os.path.exists(data_file):
        print("unit data file not exists!")
        return
    data_content = json.load(open(data_file, "r", encoding="utf-8"))
    init_unit_prefix(data_content["prefix"])
    init_unit_basic_dimension(data_content["basic_dimension"])
    init_unit_dimension(data_content["dimension"])
    init_unit_unit(data_content["unit"])
    init_unit_quantity(data_content["quantity"])
    init_quantity_with_all_unit()

    print("unit init Done!")
